//1.需要导入Vue
import Vue from "vue";
//2.导入vuex
import Vuex from "vuex"
import axios from "axios"
//3.将Vuex配置为Vue插件
Vue.use(Vuex)


//4.实例化store对象
const store = new Vuex.Store({
    //state作用于 存放 全局需要共享的数据
    state: {
        count: 0,
        books: [],
        todos: [{
                id: 1,
                name: '吃饭1',
                isDone: false
            },
            {
                id: 2,
                name: '吃饭2',
                isDone: false
            },
            {
                id: 3,
                name: '吃饭3',
                isDone: true
            },
            {
                id: 4,
                name: '吃饭4',
                isDone: false
            },
        ]
    },
    mutations: {
        updateCount(state) {
            // console.log(state);
            state.count++
        },
        manCount(state, paryload) {
            state.count += paryload
        },
        uppdateBooks(state, paryload) {
            state.books = paryload
        }
    },
    actions: {
        async getBooks(cxt) {
            const {
                data: res
            } = await axios.get("http://ajax-base-api-t.itheima.net/api/getbooks")
            cxt.commit("uppdateBooks", res.data)
        },
        // asyncCount(context, paryload) {
        //     setTimeout(() => {
        //         console.log(paryload);
        //     }, 1000)
        // }
        async getBooks2(cxt) {
            const {
                data: res
            } = await axios.get("http://ajax-base-api-t.itheima.net/api/getbooks")
            console.log(res);
            // cxt.commit("uppdateBooks", res.data)
        },
    },
    getters: {
        allDone(state) {
            return state.todos.every(item => item.isDone)
        }
    }

})
//5.向外导出store对象
export default store